package com.ywjt.platformbusiness.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.ywjt.platformbusiness.R;
import com.ywjt.platformbusiness.util.ToastUtil;


public class CenterDialogPriceModify extends Dialog {

    private Context context;
    private String price="",rate;
    private EditText etPrice,etRate;
    private TextView tvConfirm;
    private String title="";
    private TextView tvTitle;

    public CenterDialogPriceModify(@NonNull Context context, String title,String price,String rate) {
        super(context);
        this.context = context;
        this.title=title;
        this.price=price;
        this.rate=rate;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        bindView();
    }

    public void bindView() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // 设置不显示键盘
        setContentView(R.layout.dialog_edit);
        tvTitle=findViewById(R.id.tvTitle);
        etPrice=findViewById(R.id.etPrice);
        etRate=findViewById(R.id.etRate);
        tvConfirm=findViewById(R.id.tvConfirm);
        tvConfirm.setOnClickListener(new clickListener());
        etPrice.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                try {
                    price=s.toString();
                }catch (Exception e){
                    price="";
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        etRate.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                try {
                    rate=s.toString()+"%";
                }catch (Exception e){
                    rate="";
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        this.setCancelable(true);
        this.setCanceledOnTouchOutside(true);

        tvTitle.setText(title);
        etPrice.setText(price);
        etRate.setText(rate);
    }

    private ClickListenerInterface clickListenerInterface;

    public interface ClickListenerInterface {
        void confirm(String price,String rate);

    }

    public void setClicklistener(ClickListenerInterface clickListener) {
        clickListenerInterface = clickListener;
    }

    private class clickListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.tvConfirm:
                    if(price.equals("")||rate.equals("")){
                        ToastUtil toastUtil=new ToastUtil(context,R.layout.popu_success1,"请输入正确修改内容",1);
                        toastUtil.show();
                        return;
                    }else {
                        clickListenerInterface.confirm(price,rate);
                    }
                    break;
            }
        }
    }
}